iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 7
0
Modern Web

把前後分離製作的網站組起來系列 第 7

Angular-整體語法到底是甚麼?

  • 分享至 

  • xImage
  •  

今天是使用 https://stackblitz.com/angular/vmgxnnvnjyr?file=src%2Fapp%2Fcart.service.ts
解釋Angular的最後一天~明天開始就要到mysql了喔~

繼續先練習KEY~後面再來解釋~/images/emoticon/emoticon08.gif
1.右鍵單擊 app 資料夾,選擇 Angular Generator 和 Component產生一個名叫 cart 的購物車元件
https://ithelp.ithome.com.tw/upload/images/20200907/20119035wai5oHmTO1.png

2.到app.module.ts貼上程式碼:

{ path: 'cart', component: CartComponent },

https://ithelp.ithome.com.tw/upload/images/20200907/20119035Ul1N5JrVWj.png

3.到top-bar.component.html貼上程式碼:

<a routerLink="/cart" class="button fancy-button">
  <i class="material-icons">shopping_cart</i>Checkout
</a>

"Checkout" 按鈕
https://ithelp.ithome.com.tw/upload/images/20200907/20119035HJVMLgkWQx.png

4.請點選“Checkout”按鈕。你會看到預設文字“cart works!”
https://ithelp.ithome.com.tw/upload/images/20200907/20119035HF5QzJ1H6p.png

5.到 cart.component.ts貼上程式碼:

import { CartService } from '../cart.service';

https://ithelp.ithome.com.tw/upload/images/20200907/201190354F1pYIvJMl.png

private cartService: CartService

https://ithelp.ithome.com.tw/upload/images/20200907/20119035gJgzG0fPpl.png

6.這裡的cart.component.ts貼上程式碼:
要貼上的比較多,最後整體是

import { FormBuilder } from '@angular/forms';

import { CartService } from '../cart.service';

@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.css']
})
export class CartComponent implements OnInit {
  items;
  checkoutForm;

  constructor(
    private cartService: CartService,
    private formBuilder: FormBuilder,
  ) {
    this.checkoutForm = this.formBuilder.group({
      name: '',
      address: ''
    });
  }

  ngOnInit() {
    this.items = this.cartService.getItems();
  }

  onSubmit(customerData) {
    // Process checkout data here
    this.items = this.cartService.clearCart();
    this.checkoutForm.reset();

    console.warn('Your order has been submitted', customerData);
  }
}

我覺得雖然說是手冊~但是看要貼在那個{}裡面是最難的
https://ithelp.ithome.com.tw/upload/images/20200907/20119035hLzqmIlySa.png

7.cart.component.html貼上程式碼:


<p>
  <a routerLink="/shipping">Shipping Prices</a>
</p>

<div class="cart-item" *ngFor="let item of items">
  <span>{{ item.name }} </span>
  <span>{{ item.price | currency }}</span>
</div>

<form [formGroup]="checkoutForm" (ngSubmit)="onSubmit(checkoutForm.value)">

  <div>
    <label for="name">
      Name
    </label>
    <input id="name" type="text" formControlName="name">
  </div>

  <div>
    <label for="address">
      Address
    </label>
    <input id="address" type="text" formControlName="address">
  </div>

  <button class="button" type="submit">Purchase</button>

</form>

https://ithelp.ithome.com.tw/upload/images/20200907/20119035YEEsAaDmzm.png


8.點選“Checkout”檢視購物車。
https://ithelp.ithome.com.tw/upload/images/20200907/20119035gYW7vqANaz.png

9.透過 assets/shipping.json 檔案提供了一些預定義的配送資料。你可以利用這些資料為購物車中的商品新增運費。
https://ithelp.ithome.com.tw/upload/images/20200907/20119035yV82ADGECL.png

10.app.module.ts啟用 HttpClient程式碼:

import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { TopBarComponent } from './top-bar/top-bar.component';
import { ProductListComponent } from './product-list/product-list.component';
import { ProductAlertsComponent } from './product-alerts/product-alerts.component';
import { ProductDetailsComponent } from './product-details/product-details.component';
import { CartComponent } from './cart/cart.component';
import { ShippingComponent } from './shipping/shipping.component';


@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    ReactiveFormsModule,
    RouterModule.forRoot([
      { path: '', component: ProductListComponent },
      { path: 'products/:productId', component: ProductDetailsComponent },
      { path: 'cart', component: CartComponent },
      { path: 'shipping', component: ShippingComponent },
    ])
  ],
  declarations: [
    AppComponent,
    TopBarComponent,
    ProductListComponent,
    ProductAlertsComponent,
    ProductDetailsComponent,
    CartComponent,
    ShippingComponent
  ],
  bootstrap: [
    AppComponent
  ]
})
export class AppModule { }

反紅不見了~
https://ithelp.ithome.com.tw/upload/images/20200907/20119035pm0iZt1VQ1.png

11.cart.service.ts程式碼:


import { HttpClient } from '@angular/common/http';
@Injectable({
  providedIn: 'root'
})
export class CartService {
  items = [];

  constructor(
    private http: HttpClient
  ) {}

  addToCart(product) {
    this.items.push(product);
  }

  getItems() {
    return this.items;
  }

  clearCart() {
    this.items = [];
    return this.items;
  }

  getShippingPrices() {
    return this.http.get('/assets/shipping.json');
  }
}

https://ithelp.ithome.com.tw/upload/images/20200907/20119035CeFr8iflpp.png
12.產生一個名為 shipping 的新元件。在檔案列表框中,右鍵單擊 app 資料夾,選擇 Angular Generator 和 Component的/shipping.component.ts程式碼:


import { CartService } from '../cart.service';

@Component({
  selector: 'app-shipping',
  templateUrl: './shipping.component.html',
  styleUrls: ['./shipping.component.css']
})
export class ShippingComponent implements OnInit {
  shippingCosts;

  constructor(
    private cartService: CartService
  ) {
  }

  ngOnInit() {
    this.shippingCosts = this.cartService.getShippingPrices();
  }

}

https://ithelp.ithome.com.tw/upload/images/20200907/20119035HgpBinNtRp.png
13.shipping.component.html程式碼:


<div class="shipping-item" *ngFor="let shipping of shippingCosts | async">
  <span>{{ shipping.type }}</span>
  <span>{{ shipping.price | currency }}</span>
</div>


https://ithelp.ithome.com.tw/upload/images/20200907/20119035oXqveyfoyv.png

/images/emoticon/emoticon08.gif

手冊裡的"購物車"完成~
其實做到後面會發現其實程式碼都很像~
等mysql+spring介紹完~
就來個本機版的"購物站"

然後再回來介紹Angular

DEAR ALL 我們明天見~/images/emoticon/emoticon24.gif


上一篇
想要看看TYPESCRIPT是在做甚麼?
下一篇
mysql操作環境的安裝~
系列文
把前後分離製作的網站組起來30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言